CHAPTER 5

Methods, Properties, and Fields

Next up are methods, properties, and fields. Methods define procedures that you can perform on an object or class, while properties enable you to enforce encapsulation by controlling access to the internal state of your objects (fields).

Methods

Methods come in two flavors: sharedand instance. If the method is an instance method, you can call it on an object. If the method is a shared method, you can call it only on the class. The difference is that instance methods have access to the instance fields of the object instance, whereas shared methods don’t have access to instance fields or methods. Shared methods can only access shared class members.

Methods can have metadata attributes attached to them, and they can also have optional modifiers attached. We discuss them throughout this chapter. These modifiers control the accessibility of the methods, as well as facets of the methods that are germane to inheritance. Every method may have a return type or parameters.

Shared Methods

You call shared methods on the class rather than on instances of the class. Shared methods only have access to the shared members of the class. You declare a method as shared by using the Shared modifier, as in the following example:

Public Class A
    Public Shared Sub SomeFunction()
        System.Console.WriteLine("SomeFunction() called")
    End Sub

    Shared Sub Main()
        A.SomeFunction()
        SomeFunction()
    End Sub
End Class

Notice that both methods in this example are shared. In the Main method, you first access the SomeFunction method using the class name. You then call the shared method without qual-ifying it. This is because the Mainand SomeFunction methods are both defined in the same class and are both shared methods. Had SomeFunction() been in another class, say Class B, then you would have had no choice but to reference the method as B.SomeFunction.

Instance Methods

Instance methods operate on objects. In order to call an instance method, you need a refer-ence to an instance of the class that defines the method. The following example shows the use of an instance method:

Public Class A
    Private x As Integer
    Private y As Integer
    Private Shared z As Integer
    Private Sub SomeOperation()
        x = 1
        Me.y = 2
        A.z = 3

        'Assigning Me in objects is an error.
        'Dim NewInstance As A = New A()
        'Me = NewInstance
    End Sub

    Shared Sub Main()
        Dim obj As A = New A()

        obj.SomeOperation()
        System.Console.WriteLine("x = {0}, y = {1}, z = {2}", obj.x, obj.y, A.z)
    End Sub
End Class

In the Main method, you create a new instance of Class A and then call the SomeOperation method through the instance of that class. Within the method body of SomeOperation(), you have access to the instance and shared fields of the class, and you can assign to them simply by using their identifiers. Even though the SomeOperation method can assign the shared field z without qualifying it, it makes for more readable code if the assignment of shared fields is qualified by the class name even in the methods of the same class. Doing so is helpful for that special someone who comes after you and has to maintain your code. And that special some-one could be you!

Notice that when you assign to y, you do so through the Me identifier. You should note a few important things about Me when used within an instance method body. It is treated as a read-only reference whose type is that of the class. Using Me, you can access the fields of the instance, as the previous example shows when assigning the value of y. Because the Me value is a read-only value, you may not assign it. If you try to do so, you’ll hear about it when the com-piler complains and fails to compile your code.